package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.manager.model.MetaTableEntity;
import com.kelvem.saas.workbanch.core.manager.request.MetaTableQueryVo;
import com.kelvem.saas.workbanch.core.manager.service.MetaTableService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 实体表控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/metaTable")
public class MetaTableController {

    @Resource
    private MetaTableService metaTableService;
    
    /**
     * 增加实体表
     * curl -X POST http://localhost:9090/metaTable/v1/add -H 'Content-Type: application/json' -d '{"id": null, "tableEnName": "table_en_name", "tableChName": "table_ch_name", "tableType": "table_type", "tableSource": "table_source", "entityName": "entity_name", "state": "state", "parentMetaTableId": 1, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTable 实体表信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) MetaTableEntity metaTable) {
        SaasAssert.notNull(metaTable);

        log.debug("MetaTableController:add:start, metaTable=【{}】", metaTable);
        Long metaTableId = metaTableService.addMetaTable(metaTable);
        log.debug("MetaTableController:add:end, metaTableId={}", metaTableId);

        return metaTableId;
    }

    /**
     * 修改实体表
     * curl -X PUT http://localhost:9090/metaTable/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "tableEnName": "table_en_name", "tableChName": "table_ch_name", "tableType": "table_type", "tableSource": "table_source", "entityName": "entity_name", "state": "state", "parentMetaTableId": 1, "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTable 实体表信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) MetaTableEntity metaTable) {
        SaasAssert.notNull(metaTable);

        log.debug("MetaTableController:update:start, metaTable=【{}】", metaTable);
        int count = metaTableService.updateMetaTable(metaTable);
        log.debug("MetaTableController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除实体表
     * curl -X DELETE 'http://localhost:9090/metaTable/v1/delete/10002'
     *
     * @param metaTableId 实体表id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{metaTableId}")
    public Integer delete(@PathVariable(required = false) Long metaTableId) {
        SaasAssert.notNull(metaTableId);

        log.debug("MetaTableController:delete:start, metaTableId={}", metaTableId);
        int count = metaTableService.deleteMetaTable(metaTableId);
        log.debug("MetaTableController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询实体表分页数据
     * curl -X POST http://localhost:9090/metaTable/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param metaTableQueryVo 查询用
     * @return Page<MetaTableEntity> 实体表分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<MetaTableEntity> queryPage(@RequestBody(required = false) MetaTableQueryVo metaTableQueryVo) {
        SaasAssert.notNull(metaTableQueryVo);

        log.debug("MetaTableController:queryPage:start, metaTableQueryVo=【{}】", metaTableQueryVo);
        Page<MetaTableEntity> page = metaTableService.queryMetaTablePage(metaTableQueryVo);
        log.debug("MetaTableController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查实体表信息
     * curl -X GET 'http://localhost:9090/metaTable/v1/get/10000'
     *
     * @param metaTableId 实体表id
     * @return MetaTable 实体表信息
     */
    @GetMapping("/v1/get/{metaTableId}")
    public MetaTableEntity getById(@PathVariable(required = false) Long metaTableId) {
        SaasAssert.notNull(metaTableId);

        log.debug("MetaTableController:getById:start, metaTableId={}", metaTableId);
        MetaTableEntity metaTable = metaTableService.getMetaTableById(metaTableId);
        log.debug("MetaTableController:getById:end, metaTable=【{}】", metaTable);

        return metaTable;
    }

    /**
     * 增加实体表
     * curl -X POST http://localhost:9090/metaTable/v1/importTable -H 'Content-Type: application/json' -d '{"id": null, "tableEnName": "sys_user", "tableChName": "系统用户", "memo": "memo"}'
     *
     * @param metaTable 实体表信息
     * @return Long 主键id
     */
    @PostMapping("/v1/importTable")
    public Long importTable(@RequestBody(required = false) MetaTableEntity metaTable) {
        SaasAssert.notNull(metaTable);

        log.debug("MetaTableController:importTable:start, metaTable=【{}】", metaTable);
        Long importTableId = metaTableService.importMetaTable(metaTable);
        log.debug("MetaTableController:importTable:end, importTableId={}", importTableId);

        return importTableId;
    }

    /**
     * 查询实体表列表数据
     * curl -X POST http://localhost:9090/metaTable/v1/queryList -H 'Content-Type: application/json' -d '{}'
     *
     * @param metaTableQueryVo 查询用
     * @return List<MetaTableEntity> 实体表分页数据
     */
    @PostMapping("/v1/queryList")
    public List<MetaTableEntity> queryList(@RequestBody(required = false) MetaTableQueryVo metaTableQueryVo) {
        SaasAssert.notNull(metaTableQueryVo);

        log.debug("MetaTableController:queryPage:start, metaTableQueryVo=【{}】", metaTableQueryVo);
        List<MetaTableEntity> list = metaTableService.queryMetaTableList(metaTableQueryVo);
        log.debug("MetaTableController:queryPage:end, list.count={}", list.size());

        return list;
    }

    /**
     * 拷贝实体表
     * curl -X POST http://localhost:9090/metaTable/v1/copy/998 -H 'Content-Type: application/json' -d '{}'
     *
     * @param id 被拷贝的实体表id
     * @return Long 拷贝生成的实体表id
     */
    @PostMapping("/v1/copy/{id}")
    public Long copy(@PathVariable(required = false) Long id) {
        SaasAssert.notNull(id);

        log.debug("MetaTableController:copy:start, sourceId=【{}】", id);
        Long newId = metaTableService.copyMetaTable(id);
        log.debug("MetaTableController:copy:end, targetId={}", newId);

        return newId;
    }

    /**
     * 拷贝实体表
     * curl -X POST http://localhost:9090/metaTable/v1/createMetaTable/998 -H 'Content-Type: application/json' -d '{}'
     *
     * @param id 被拷贝的实体表id
     * @return OK
     */
    @PostMapping("/v1/createMetaTable/{id}")
    public String createMetaTable(@PathVariable(required = false) Long id) {
        SaasAssert.notNull(id);

        log.debug("MetaTableController:createMetaTable:start, id=【{}】", id);
        metaTableService.createMetaTable(id);
        log.debug("MetaTableController:createMetaTable:end");

        return "OK";
    }
    
}
